package ha.biz.assign;

import ha.biz.AbstractBizManagerTests;

import java.util.Date;
import java.util.Map.Entry;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.smartwebx.ha.biz.assign.HaUserManager;
import com.smartwebx.ha.dal.assign.bean.HaUser;
import com.smartwebx.ha.usebean.Paging;
import com.smartwebx.ha.usebean.TableData;

public class HaUserTester extends AbstractBizManagerTests {
	@Autowired
	private HaUserManager haUserM;

	private int userId;

	@Before
	public void setUp() {
		HaUser haUser = new HaUser();
		haUser.setAreaId(1);
		haUser.setRoleId(1);
		haUser.setIsLogin(true);
		haUser.setEmail("cycy126@126.com");
		haUser.setMobile("1234567");
		haUser.setPass("1234567");
		haUser.setCreateTime(new Date());
		haUser.setUpdateTime(new Date());
		haUser.setName("zhangxi");
		userId = haUserM.addUserGetId(haUser);

		Assert.assertTrue(userId > 0);
	}

	@After
	public void setDown() {
		this.testDel();
	}

	@Test
	public void testMain() {
		this.testQueryByPage();
		this.testLoginUser();
		this.testUpdateUser();
		this.testCheckName();
		this.testChangePass();
	}

	public void testChangePass() {
		boolean sign = haUserM.changePass(userId, "12345678");
		Assert.assertTrue(sign);
	}

	public void testQueryByPage() {
		Paging paging = new Paging();
		paging.setPageIndex(0);
		paging.setPageSize(3);
		paging.setSortField("createTime");
		paging.setSortOrder("desc");

		TableData<HaUser> users = haUserM.queryUserByPage(paging,1,null);
		Assert.assertEquals("zhangxi", users.getData().get(0).getName());
	}

	/**
	 * 测试用户登陆，用户锁定
	 */
	public void testLoginUser() {
		String userName = "zhangxi";
		String pass = "1234567";
		Entry<Integer, HaUser> loginUser = haUserM.loginUser(userName, pass);
		// 测试用户名密码正确时的登陆情况
		Assert.assertTrue(1 == loginUser.getKey());

		pass = "123456";
		loginUser = haUserM.loginUser(userName, pass);
		// 测试密码错误的登陆情况
		Assert.assertTrue(0 == loginUser.getKey());

		// 锁定用户
		haUserM.lockedUser(userId);
		pass = "1234567";
		loginUser = haUserM.loginUser(userName, pass);
		// 测试用户锁定的登陆情况
		Assert.assertTrue(-1 == loginUser.getKey());
	}

	public void testUpdateUser() {
		String email = "zhang.xi.unbug@gmail.com";
		HaUser updateHaUser = new HaUser();
		updateHaUser.setId(userId);
		updateHaUser.setEmail(email);
		haUserM.updateUser(updateHaUser);

		HaUser haUser = haUserM.findById(userId);

		Assert.assertEquals(email, haUser.getEmail());
	}

	public void testCheckName() {
		String name = "zhangxi";
		boolean sign = haUserM.checkName(name);
		// 有用户名重复
		Assert.assertFalse(sign);
	}

	public void testDel() {
		boolean sign = haUserM.delUser(userId);
		Assert.assertTrue(sign);
	}

}